package cn.marinda.resturants.controller;

import cn.marinda.resturants.entity.Bill;
import cn.marinda.resturants.enums.EntityJson;
import cn.marinda.resturants.global.Pages;
import cn.marinda.resturants.service.BillService;
import cn.marinda.resturants.utils.ApiResult;
import cn.marinda.resturants.utils.IOUtils;
import cn.marinda.resturants.utils.PagesResult;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

/** 账单控制器
 * @version 1.0
 * @author Marinda
 *
 */
@RestController
@RequestMapping("/bill")
public class BillController {

    @Resource
    private BillService billService;

    /** 返回列表
     *
     * @date 2021/12/1
     * @return 结果
     */
    @RequestMapping("/queryList")
    private ApiResult queryList()
    {
        return new ApiResult(ApiResult.ON_SUCCESS,ApiResult.SUCCESS_MESSAGE,billService.queryList());
    }

    /** 返回列表
     *
     * @date 2021/12/1
     * @return 结果
     */
    @RequestMapping("/index")
    private ApiResult selectByUid(String uid)
    {
        return new ApiResult(ApiResult.ON_SUCCESS,ApiResult.SUCCESS_MESSAGE, Arrays.asList(billService.selectByUid(uid)));
    }

    /** 列表分页
     *
     * @date 2021/12/1
     * @return 结果
     */
    @RequestMapping("/pages")
    private PagesResult selectPages(@RequestParam("page") int page,@RequestParam("limit") int limit)
    {
        IPage<Bill> billIPage = billService.selectBillPage(new Pages<>(page,limit));
        return new PagesResult(0,billIPage.getRecords(),(int)billIPage.getTotal(),billIPage.getSize());
    }


    /** 添加账单
     *
     * @param bill 账单
     * @return 结果
     */
    @RequestMapping("/add")
    private ApiResult addBill(Bill bill){
        return billService.addBill(bill) ? new ApiResult(ApiResult.ON_SUCCESS,ApiResult.SUCCESS_MESSAGE,null) : new ApiResult(ApiResult.ON_FAIL,ApiResult.FAIL_MESSAGE,null);
    }

    @RequestMapping("/delete")
    private ApiResult deleteBill(String uid){
        return billService.deleteBill(uid) ? new ApiResult(ApiResult.ON_SUCCESS,ApiResult.SUCCESS_MESSAGE,null) : new ApiResult(ApiResult.ON_FAIL,ApiResult.FAIL_MESSAGE,null);
    }


    /**
     * 查询一个admin_uid的账单
     *
     * @param uid uid
     * @return
     */
    @RequestMapping("/adminList")
    public ApiResult selectByAdminUid(String uid) {
        List<Bill> billList = billService.selectByAdminUid(uid);
        return new ApiResult(ApiResult.ON_SUCCESS,ApiResult.SUCCESS_MESSAGE,billList);
    }

    /**
     * 查询所有type属性下的账单
     *
     * @param type 属性类型
     * @return 集合类型
     */
    @RequestMapping("/typeList")
    public ApiResult selectTypeList(String type) {
        return new ApiResult(ApiResult.ON_SUCCESS,ApiResult.SUCCESS_MESSAGE,billService.selectTypeList(type));
    }

    /**
     * number数
     *
     * @param adminUid
     * @return
     */
    @RequestMapping("/number")
    public ApiResult selectBillNumber(String adminUid) {
        return new ApiResult(ApiResult.ON_SUCCESS,ApiResult.SUCCESS_MESSAGE,billService.selectBillNumber(adminUid));
    }

    /**
     * 乐观锁修改账单
     *
     * @param bill 账单
     * @return 结果
     */
    @RequestMapping("/update")
    public ApiResult updateBill(Bill bill) {
        return billService.updateBill(bill) ? new ApiResult(ApiResult.ON_SUCCESS,ApiResult.SUCCESS_MESSAGE,null) : new ApiResult(ApiResult.ON_FAIL,ApiResult.FAIL_MESSAGE,null);

    }
}
